package com.we.risk.logoff.userinfo;

import com.alibaba.fastjson.JSONObject;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.RichFlatMapFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.util.Collector;

public class TableHhl01UserPid {
    public static final String KFKTABLE = "fdm.hhl01_user_pid_230522";
    public static final String QUERYSQL =
            "select uid, pid, updated_at "
                    + " from "
                    + KFKTABLE
                    + " where deleted_at is null "
                    + "and updated_at is not null "
                    + "and pid is not null";

    public long uid;
    public String pid;
    public int updated_at;

    public TableHhl01UserPid() {
    }

    public TableHhl01UserPid(long uid, String pid, int updated_at) {
        this.uid = uid;
        this.pid = pid;
        this.updated_at = updated_at;
    }

    public static TableHhl01UserPid init(long uid) {
        return new TableHhl01UserPid(uid, "", 0);
    }
    public static SingleOutputStreamOperator<String> getTableHhl01UserPidStream(
            StreamTableEnvironment tableEnv, StreamExecutionEnvironment env) {
        Table tableRes = tableEnv.sqlQuery(TableHhl01UserPid.QUERYSQL);
        DataStream<Tuple2<Boolean, TableHhl01UserPid>> inputStream =
                tableEnv.toRetractStream(tableRes, TableHhl01UserPid.class);
        SingleOutputStreamOperator<String> out = inputStream.filter(new FilterFunction<Tuple2<Boolean, TableHhl01UserPid>>() {
            @Override
            public boolean filter(Tuple2<Boolean, TableHhl01UserPid> value) throws Exception {
                return value.f0;
            }
        }).uid("TableHhl01UserPid-filter")
                .keyBy(new KeySelector<Tuple2<Boolean, TableHhl01UserPid>, Long>() {
                    @Override
                    public Long getKey(Tuple2<Boolean, TableHhl01UserPid> value) throws Exception {
                        return value.f1.getUid();
                    }
                }).flatMap(new RichFlatMapFunction<Tuple2<Boolean, TableHhl01UserPid>, String>() {
                    ValueState<TableHhl01UserPid> tableHhl01UserPidVState;

                    @Override
                    public void open(Configuration parameters) throws Exception {
                        tableHhl01UserPidVState = getRuntimeContext()
                                .getState(new ValueStateDescriptor<TableHhl01UserPid>("TableHhl01UserPidVState", TableHhl01UserPid.class));
                    }

                    @Override
                    public void flatMap(Tuple2<Boolean, TableHhl01UserPid> value, Collector<String> out) throws Exception {
                        long uid = value.f1.getUid();
                        String pid = value.f1.getPid();
                        int updated_at = value.f1.getUpdated_at();

                        TableHhl01UserPid tmpState = tableHhl01UserPidVState.value();
                        TableHhl01UserPid curState = tmpState == null ? init(uid) : tmpState;

                        if (updated_at > curState.getUpdated_at()) {
                            if (!pid.equals(curState.getPid())) {
                                curState = value.f1;
                                JSONObject outJson = new JSONObject();
                                outJson.put("uid", uid);
                                outJson.put("pid", pid);

                                out.collect(outJson.toString());
                            }
                            curState.updated_at = updated_at;
                            tableHhl01UserPidVState.update(curState);
                        }
                    }
                });

        return out;
    }

    public long getUid() {
        return uid;
    }

    public String getPid() {
        return pid;
    }

    public int getUpdated_at() {
        return updated_at;
    }
}
